import sys
import re
import urllib2
import urlparse
import webapp2
import logging

from google.appengine.api import users

from model import Course

crawling = 'https://webrb.nus.edu.sg/examtt/Exam2012/Semester%202/MASTER%20Sem%202%20by%20Module.html'

def crawl():
	try:
		response = urllib2.urlopen(crawling, timeout=1800)
	except DownloadError:
		logging.error(DownloadError)

	msg = response.read()
	keywordregex = re.compile('<TD><FONT size=-1>(.*?)</FONT></TD>',re.S)
	keyword_table_regex = re.compile('<TD><FONT size=-1>(.*?)</TBODY></TABLE>',re.S)
	
	logging.info(len(msg))
	counter = 0;
	courseholder = {}
	for entry in keywordregex.findall(msg):
		if counter == 0:
			courseholder['faculty'] = entry.upper()
		elif counter == 1:
			courseholder['openbook'] = (entry == "*")
		elif counter == 2:
			courseholder['code'] = entry.upper()
		elif counter == 3:
			courseholder['title'] = ' '.join(entry.split()).decode("windows-1252").replace("&amp;","&").upper()
		elif counter == 4:
			courseholder['date'] = ' '.join(entry.split())
		elif counter == 5:
			courseholder['time'] = ' '.join(entry.split())
		elif counter == 6:
			courseholder['duration'] = ' '.join(entry.split())
		elif counter == 7:
			courseholder['venue'] = ' '.join(entry.split())
			try:
				if
				course = Course(parent=None,key_name=courseholder['code'],
								faculty=courseholder['faculty'],
								is_open_book=courseholder['openbook'],
								title=courseholder['title'],
								time=courseholder['date']+' '+courseholder['time'],
								duration=courseholder['duration'],
								venue=courseholder['venue'],
								views=0)
				course.put()
			except:
				print courseholder
		counter = (counter+1)%8;

def isauthenticated():
	if users.get_current_user() and users.get_current_user().nickname() != '2ndfromleft':
		print users.get_current_user().nickname()
		return False
	else:
		return True

class Crawler(webapp2.RequestHandler):

	def get(self):
		self.response.headers['Content-Type'] = 'text/plain'
		if not isauthenticated():
			self.response.write("restricted")
		else:
			crawl()
			self.response.write("done")

class Cleaner(webapp2.RequestHandler):
	def get(self):
		self.response.headers['Content-Type'] = 'text/plain'
		if not isauthenticated():
			self.response.write("restricted")
		else:
			Course.emptydatastore()
			self.response.write("done")